﻿using HirelSeriesH.Tools;
using MySql.Data.MySqlClient;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HirelSeriesH
{
    public partial class CreateEngineerUserForm : UIForm
    {
        private string CreateBy;
        public CreateEngineerUserForm(string username)
        {
            this.CreateBy = username;
            InitializeComponent();
        }

        private void BtnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            string userName = this.TextUserName.Text.Trim();
            if (userName == null || userName == "")
            {
                UIMessageDialog.ShowMessageDialog(this, "请输入用户名", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }
            //反查数据库，看用户名是否已被占用
            MySqlParameter[] paras = new MySqlParameter[] { new MySqlParameter("@UserName", userName) };
            object result = DBHelper.ExecuteScalar(DBSql.SELECT_USERNAME, 1, paras);
            int count = int.Parse(result.ToString());
            //如果是第一次启动温箱，则现在数据库中建立温箱记录。
            if (count != 0)
            {
                UIMessageDialog.ShowMessageDialog(this, "用户名已被占用", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }

            //用户名6-16位、只能包含英文字母和数字
            var regex = new Regex("^[A-Za-z0-9]{6,16}$", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
            //校验用户名是否符合要求
            bool userNameIsMatch = regex.IsMatch(userName);
            if (!userNameIsMatch)
            {
                UIMessageDialog.ShowMessageDialog(this, "用户名长度为6-16位，且只能包含英文字母和数字。", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }
            //查询ENGINEER角色的ID
            result = DBHelper.ExecuteScalar(DBSql.SELECT_HTRB_ROLE_ENGINEER, 1);
            string EngineerRoleID = result.ToString();
            
            IdWorker idWorker = new IdWorker(1, 1);
            string engineerUserID = idWorker.NextId().ToString();
            //取当前时间并格式化
            DateTime time = DateTime.Now;
            string format = "yyyy-MM-dd HH:mm:ss"; 
            string CreateDate = time.ToString(format);
            //插入新的ENGINEER用户记录,默认密码666666
            paras = new MySqlParameter[] { new MySqlParameter("@ID", engineerUserID), new MySqlParameter("@UserName", userName), new MySqlParameter("@PassWord", "666666"),
            new MySqlParameter("@CreateBy", this.CreateBy), new MySqlParameter("@CreateDate", CreateDate), new MySqlParameter("@UpdateBy", null), new MySqlParameter("@UpdateDate", null)};
            DBHelper.ExecuteNonQuery(DBSql.INSERT_HTRB_USER, 1, paras);
            //分配角色
            paras = new MySqlParameter[] { new MySqlParameter("@ID", idWorker.NextId().ToString()), new MySqlParameter("@RoleID", EngineerRoleID), new MySqlParameter("@UserID", engineerUserID),
            new MySqlParameter("@CreateBy", this.CreateBy), new MySqlParameter("@CreateDate", CreateDate), new MySqlParameter("@UpdateBy", null), new MySqlParameter("@UpdateDate", null)};
            DBHelper.ExecuteNonQuery(DBSql.INSERT_HTRB_USER_ROLE, 1, paras);

            UIMessageDialog.ShowMessageDialog(this, "新建用户成功,默认密码666666，请提醒用户即使修改默认密码。", UILocalize.InfoTitle, false, UIStyle.Colorful);
            this.Close();

        }
    }
}
